﻿using Common;
using Model;
using SQLServerHelper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Library_CommonData
{
    public class CacheData
    {

        private static string sql = "";
        private static DataTable dtTableColumns = null;
        private static DataTable ListTableColumns = null;
        private static SqlHelper sqlHelper = new SqlHelper();
        static SQLExceptionLog LogHelper = new SQLExceptionLog();

        /// <summary>
        /// 表字段集合
        /// </summary>
        public static List<Syscolumns> syscolumnsList = new List<Syscolumns>();

        /// <summary>
        /// 编程语言集合
        /// </summary>
        public static List<tb_Language> languageList = new List<tb_Language>();


        public CacheData(string tableName)
        {
            InitData(tableName);
        }

        /// <summary>
        /// 数据初始化
        /// </summary>
        public static void InitData(string tableName)
        {
            syscolumnsList = syscolumnsList == null || syscolumnsList.Count == 0 ? GetColumnsList(tableName) : syscolumnsList;
            languageList = languageList == null || languageList.Count == 0 ? GetLanguageList() : languageList;
            
        }

        /// <summary>
        /// 获取表字段名称
        /// </summary>
        public static List<Syscolumns> GetTableColumnsNameList(string tableName)
        {
            //string columnsStr = "*";

            try
            {

                InitData(tableName);
                //if (syscolumnsList != null && syscolumnsList.Count > 0)
                //{
                //    columnsStr = "";
                //    foreach (Syscolumns item in syscolumnsList)
                //    {
                //        columnsStr += item.name + ",";
                //    }
                //    columnsStr = columnsStr.Trim(',');
                //}


            }
            catch (Exception ex)
            {
                LogHelper.AddThrowErrorbLog("", ex.ToString(), "");
            }
            return syscolumnsList;


        }


        /// <summary>
        /// 获取表字段名称
        /// </summary>
        public static string GetTableColumnsNameStr(string tableName)
        {
            string columnsStr = "*";

            try
            {

                InitData(tableName);
                if (syscolumnsList != null && syscolumnsList.Count > 0)
                {
                    columnsStr = "";
                    foreach (Syscolumns item in syscolumnsList)
                    {
                        columnsStr += item.name + ",";
                    }
                    columnsStr = columnsStr.Trim(',');
                }


            }
            catch (Exception ex)
            {
                LogHelper.AddThrowErrorbLog("", ex.ToString(), "");
            }
            return columnsStr;


        }
   
        /// <summary>
        /// 获取编程语言ID根据名称
        /// </summary>
        /// <param name="languageName">语言名称</param>
        /// <returns></returns>
        public static int GetLanguageIdByName(string languageName)
        {
            int languageID = 0;
            try
            {
                InitData("tb_Language");
                languageID = languageList.Where(l=>l.languageName==languageName).Select(s=>s.id).FirstOrDefault();

            }
            catch (Exception ex)
            {
                LogHelper.AddThrowErrorbLog("", ex.ToString(), "");
            }

            return languageID;
        }

        /// <summary>
        /// 获取编程语言集合
        /// </summary>
        private static List<tb_Language> GetLanguageList()
        {
            try
            {
                sql = @"SELECT id,languageName FROM dbo.tb_Language";
               
                dtTableColumns = sqlHelper.SqlExecuteDataTable(sql);
                languageList = (List<tb_Language>)ModelConvertHelper<tb_Language>.ConvertToModel(dtTableColumns);
            }
            catch (Exception ex)
            {
                LogHelper.AddThrowErrorbLog("", ex.ToString(), "");
            }

            return languageList;
        }

        /// <summary>
        /// 获取表字段名称通过表名
        /// </summary>
        private static List<Syscolumns> GetColumnsList(string tableName)
        {
            try
            {
                sql = @"select id, name,colid from syscolumns where id=object_id('" + tableName + "') order by colid asc";
                //sql = @"select name from syscolumns where id=object_id('@tableName') order by colid asc";
                //SqlParameter[] parame = new SqlParameter[] { new SqlParameter("@tableName", tableName) };

                dtTableColumns = sqlHelper.SqlExecuteDataTable(sql);
                syscolumnsList = (List<Syscolumns>)ModelConvertHelper<Syscolumns>.ConvertToModel(dtTableColumns);
            }
            catch (Exception ex)
            {
                LogHelper.AddThrowErrorbLog("", ex.ToString(), "");
            }

            return syscolumnsList;
        }


    }
}
